#pragma once

#include "LidarDataManager.h"
#include "LidarConfigurationOptions.h"

#include <Poco/Logger.h>
#include <Poco/FileChannel.h>
#include <Poco/FormattingChannel.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Util/IniFileConfiguration.h>
#include <Poco/SharedPtr.h>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/Thread.h>

#include <pcl/compression/octree_pointcloud_compression.h>

#include <mutex>
#include <queue>

class DataSaver
{
public:
    DataSaver(std::string config_file, Poco::Int32 id);
    ~DataSaver();

    void start();
    void stop();

private:
    Poco::Int32 _id;
    ILidarDataManager *_lidarDataManager;

    bool _is_saving = false;
    Poco::Thread _threadSaveAsStream;
    static void threadSaveAsStream(void *p);
    void saveAsStream();

    pcl::io::OctreePointCloudCompression<pcl::PointXYZ> *_encoder = nullptr;
    std::mutex _mtx_saver;
    std::string _save_file_path;
    bool processData(PointCloudData<pcl::PointXYZI> &data);
    std::queue<PointCloudData<pcl::PointXYZI>> cloud_queue;

    double lastTimeStamp = 0;
    double startTimeStamp = 0;
    int encodeNum = 0;
    std::stringstream _stream;
    std::vector<std::pair<uint64_t, std::streampos>> stream_index;
};